﻿using System;
using System.Net;
using System.IO;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.UI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

/// <summary>
///mrjapp 的摘要说明
/// </summary>
public class AppCan
{
    string authtoken = "748d1f13-be35-4255-c415-d079209f7e9e";
    string email = "381178259@qq.com";
    static string getmsg;//保存token凭证
    MyDbHelper mydb = new MyDbHelper();
    public AppCan()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}
    public void SendShopMsg(string stype,string title,string msg)
    {
        string appid = "11530692";
        string body = "{\"msgName\":\""+msg+"\"}";
        //string url = "http://newpush.appcan.cn/oauth/sendByUid?authtoken=" + authtoken
        //    + "&email=" + email + "&appId=" + appid + "&platforms=1&userIdListStr=2054&title=" + title+body;
        string url = "http://newpush.appcan.cn/oauth/sendByUid";
        string param = "authtoken=" + authtoken
                    + "&email=" + email
                    + "&appId=" + appid
                    + "&platforms=1"
                    + "&userIdListStr=2054"
                    + "&title=" + title
                    + "&body=" + body;
        /*
         authtoken 授权token ——— 例如：deb12c85-b675-b77b-2cfd-f6f0cd7219a5
         email 用户邮箱 ———例如：test@test.com
         appId 应用Id ——— 例如：11387747
         platforms 推送平台，1：android;0：ios，默认0，1
         userIdListStr 推送用户的userId列表, 例如：1,2,3,4 （每一个userId用逗号隔开）userId相关接口为uexWidget.setPushInfo(uId, uNickName)方法的第一个参数，参考文档 点击跳转 uexWidget。
         title 推送消息内容
         body {“msgName”:”推送消息标题”} —- 必须为json数据
        */
        PostModel(url, param);

    }
    public string JPrushSendMsg(string title ,string msg,string userlist)
    {
        string appkey = "b467e54131ce30c39498dae5";
        string masterSecret = "238ce6367cc35a2e28db93a8";
        //string Authorization = "YjQ2N2U1NDEzMWNlMzBjMzk0OThkYWU1JTNBMjM4Y2U2MzY3Y2MzNWEyZTI4ZGI5M2E4";
        string url ="https://api.jpush.cn/v3/push";
        string baseString = appkey + ":" + masterSecret;
        byte[] bytes = Encoding.Default.GetBytes(baseString);
        baseString = "Basic " + Convert.ToBase64String(bytes);

		// print_r($header);
		string param="{\"platform\":\"all\","
                    + "\"audience\":{ \"alias\" : [ " + userlist + " ]},"
                    + "\"notification\" : {\"alert\" : \""+msg+"\"},"
                    + "\"message\":{\"msg_content\":\"" + msg + "\",\"title\":\"" + title + "\",\"extras\": {\"key\": \"2001\"}}}";
        string relust = PostModel2(url,param,baseString);
        return relust;
    }
    public string PostModel2(string strUrl, string strParm, string baseString)
    {
        Encoding encode = System.Text.Encoding.UTF8;
        byte[] arrB = encode.GetBytes(strParm);

        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
        myReq.Method = "POST";
        //myReq.Headers.Add("Content-Type", "Content-Type:application/json");

        myReq.ContentType = "application/json";
        myReq.Headers.Add("Authorization", baseString);
        //myReq.ContentType = "text/xml";
        myReq.ContentLength = arrB.Length;
        Stream outStream = myReq.GetRequestStream();
        outStream.Write(arrB, 0, arrB.Length);
        outStream.Close();
        WebResponse myResp = null;
        try
        {
            //接收HTTP做出的响应
            myResp = (HttpWebResponse)myReq.GetResponse();
        }
        catch (Exception e)
        {
            int ii = 0;
        }
        Stream ReceiveStream = myResp.GetResponseStream();
        StreamReader readStream = new StreamReader(ReceiveStream, encode);
        Char[] read = new Char[256];
        int count = readStream.Read(read, 0, 256);
        string str = null;
        while (count > 0)
        {
            str += new String(read, 0, count);
            count = readStream.Read(read, 0, 256);
        }
        readStream.Close();
        myResp.Close();
        return str;
    }
    public string strtojson(string jo)
    {
        string tempstr = "[";
        jo = jo.Substring(1, jo.Length - 2);
        string[] str = jo.Split(',');
        foreach (string s in str)
        {
            tempstr += "{\"getdate\":" + s.Substring(1, s.IndexOf("\":") - 1) + "\",\"qty\"" + s.Substring(s.IndexOf("\":") + 1) + "},";
        }
        tempstr = tempstr.TrimEnd(',')+"]";

        return tempstr;
    }
    /// <summary>
    /// get方法
    /// </summary>
    /// <param name="strUrl">传入url</param>
    /// <returns></returns>
    public string GetModel(string strUrl)
    {
        string strRet = null;
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
            request.Timeout = 2000;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            System.IO.Stream resStream = response.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            StreamReader readStream = new StreamReader(resStream, encode);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            while (count > 0)
            {
                String str = new String(read, 0, count);
                strRet = strRet + str;
                count = readStream.Read(read, 0, 256);
            }
            resStream.Close();
        }
        catch (Exception e)
        {
            strRet = "";
        }
        return strRet;
    }
    /// <summary>
    /// post方法
    /// </summary>
    /// <param name="strUrl">传入url</param>
    /// <param name="strParm">传入参数</param>
    /// <returns></returns>
    public string PostModel(string strUrl, string strParm)
    {
        Encoding encode = System.Text.Encoding.UTF8;
        byte[] arrB = encode.GetBytes(strParm);

        HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(strUrl);
        myReq.Method = "POST";
        myReq.ContentType = "application/x-www-form-urlencoded";
        //myReq.ContentType = "text/xml";
        myReq.ContentLength = arrB.Length;
        Stream outStream = myReq.GetRequestStream();
        outStream.Write(arrB, 0, arrB.Length);
        outStream.Close();
        WebResponse myResp = null;
        try
        {
            //接收HTTP做出的响应
            myResp = (HttpWebResponse)myReq.GetResponse();
        }
        catch (Exception e)
        {
            int ii = 0;
        }
        Stream ReceiveStream = myResp.GetResponseStream();
        StreamReader readStream = new StreamReader(ReceiveStream, encode);
        Char[] read = new Char[256];
        int count = readStream.Read(read, 0, 256);
        string str = null;
        while (count > 0)
        {
            str += new String(read, 0, count);
            count = readStream.Read(read, 0, 256);
        }
        readStream.Close();
        myResp.Close();
        return str;
    }


 /// <summary>
    /// token的实体类
    /// </summary>
    public class JsonObject_token
    {
        public string access_token { get; set; }
        public string expires_in { get; set; }
    }

 /// <summary>
    /// jstoken的实体类
    /// </summary>
    public class JsonObject_jstoken
    {
        public string errcode { get; set; }
        public string errmsg { get; set; }
        public string ticket { get; set; }
        public string expires_in { get; set; }
        public string scope { get; set; }
    }
}